package top.itaso.lib.common;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

/**
 * @Description:
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @CreateDate: 2022/1/17
 */
public class ToastUtils {

    /**
     * 实现不管我们触发多少次 Toast 调用，都只会持续一次Toast显示的时长
     */
    private Toast mToast;
    private Toast mToastCenter;

    private static ToastUtils instance;

    public static ToastUtils getInstance() {
        if (instance == null) {
            instance = new ToastUtils();
        }
        return instance;
    }


    /**
     * 短时间显示Toast【居下】
     *
     * @param msg 显示的内容-字符串
     */
    public void showShortToast(Context context, String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * 短时间显示Toast【居中】
     *
     * @param msg 显示的内容-字符串
     */
    public void showShortToastCenter(Context context, String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mToastCenter == null) {
            mToastCenter = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            mToastCenter.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mToastCenter.setText(msg);
        }
        mToastCenter.setDuration(Toast.LENGTH_SHORT);
        mToastCenter.show();
    }

    /**
     * 长时间显示Toast【居下】
     *
     * @param msg 显示的内容-字符串
     */
    public void showLongToast(Context context, String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        } else {
            mToast.setText(msg);
        }
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.show();
    }

    /**
     * 长时间显示Toast【居中】
     *
     * @param msg 显示的内容-字符串
     */
    public void showLongToastCenter(Context context, String msg) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        if (mToastCenter == null) {
            mToastCenter = Toast.makeText(context, msg, Toast.LENGTH_LONG);
            mToastCenter.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mToastCenter.setText(msg);
        }
        mToastCenter.setDuration(Toast.LENGTH_LONG);
        mToastCenter.show();
    }

}
